import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * 
 */

/**
 * @author sdvina
 * 
 */
@SuppressWarnings("serial")
public class MyCanvas extends JPanel implements MouseListener {
	// 动作监听器的标志位，True 为已经添加的动作事件，false是尚未添加的动作事件
	boolean hasAddActionListener = false;
	Cell[] cell;
	Rectangle cellNull;
	public static int pictureID = 1;

	public MyCanvas() {
		this.setLayout(null);
		this.setSize(400, 400);
		cellNull = new Rectangle(200, 200, 100, 100);
		cell = new Cell[9];
		Icon icon;
		// 为九个方格加载图片，并初始化坐标，形成3行3列
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				icon = new ImageIcon(MyCanvas.class.getResource("picture/pic_"
						+ pictureID + "_" + (i * 3 + j + 1) + ".jpg"));
				cell[i * 3 + j] = new Cell(icon);
				// 设置按钮的位置
				cell[i * 3 + j].setLocation(j * 100, i * 100);
				this.add(cell[i * 3 + j]);
			}
		}
		this.remove(cell[8]);
	}

	// 当选择其它图形拼图时，需要重新加载新图片
	public void reloadPicture() {
		Icon icon;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				icon = new ImageIcon(MyCanvas.class.getResource("picture/pic_"
						+ pictureID + "_" + (i * 3 + j + 1) + ".jpg"));
				cell[i * 3 + j].setIcon(icon);
			}
		}
	}

	// 回复到开始有序状态
	public void rollBack() {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (i * 3 + j == 8)
					break;
				cell[i * 3 + j].setLocation(j * 100, i * 100);
				this.add(cell[i * 3 + j]);
			}
		}
		cellNull.setLocation(200, 200);

	}

	// 判断拼图是否完成
	public boolean isFinish() {
		for (int i = 0; i < 8; i++) {
			int x = cell[i].getBounds().x;
			int y = cell[i].getBounds().y;
			if (y / 100 * 3 + x / 100 != i) {
				return false;
			}
		}
		return true;
	}

	// 对方格进行重新排列，打乱顺序
	public void Start() {
		Random r = new Random();
		int[] arr = new int[9];
		int[] temp = new int[9];
		for (int i = 0; i < temp.length; i++) {
			temp[i] = i + 1;
		}
		for (int i = 0; i < arr.length; i++) {
			int index = r.nextInt(temp.length - i);
			arr[i] = temp[index];
			temp[index] = temp[temp.length - i - 1];
		}
		for (int i = 0; i < arr.length; i++) {
			switch (arr[i]) {
			case 1:
				cell[i].setLocation(0, 0);
				break;
			case 2:
				cell[i].setLocation(0, 100);
				break;
			case 3:
				cell[i].setLocation(0, 200);
				break;
			case 4:
				cell[i].setLocation(100, 0);
				break;
			case 5:
				cell[i].setLocation(100, 100);
				break;
			case 6:
				cell[i].setLocation(100, 200);
				break;
			case 7:
				cell[i].setLocation(200, 0);
				break;
			case 8:
				cell[i].setLocation(200, 100);
				break;
			case 9:
				cell[i].setLocation(200, 200);
				break;
			}
		}
		cellNull.setLocation(cell[8].getLocation().x, cell[8].getLocation().y);
		this.remove(cell[8]);
		// 为按钮添加事件
		if (!hasAddActionListener)
			for (int i = 0; i < 8; i++)
				cell[i].addMouseListener(this);
		hasAddActionListener = true;

	}

	// 鼠标按下时的事件
	public void mousePressed(MouseEvent arg0) {
		Cell button = (Cell) arg0.getSource();
		int x1 = button.getBounds().x;
		int y1 = button.getBounds().y;
		// 获取空格坐标
		int x2 = cellNull.getBounds().x;
		int y2 = cellNull.getBounds().y;
		// 进行方向移动判断
		if (x1 == x2 && y1 - y2 == 100)
			button.move("UP", 100);
		else if (x1 == x2 && y1 - y2 == -100)
			button.move("DOWN", 100);
		else if (x1 - x2 == 100 && y1 == y2)
			button.move("LEFT", 100);
		else if (x1 - x2 == -100 && y1 == y2)
			button.move("RIGHT", 100);
		else
			return;
		cellNull.setLocation(x1, y1);
		this.repaint();
		if (this.isFinish()) {
			JOptionPane.showMessageDialog(this, "完成拼图！");
			for (int i = 0; i < 8; i++) {
				cell[i].removeMouseListener(this);
				hasAddActionListener = false;
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

}
